package cn.jboa.action;

import cn.jboa.common.Constants;
import cn.jboa.entity.BizClaimVoucher;
import cn.jboa.entity.BizClaimVoucherDetail;
import cn.jboa.entity.SysEmployee;
import cn.jboa.service.CliamVouCherService;
import cn.jboa.service.impl.CliamVouCherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.*;

public class ClaimVouCherAction extends BaseAction<BizClaimVoucher> {
    private BizClaimVoucher claimVoucher;
    private List<BizClaimVoucherDetail> detailList;
    private String status;
    private Date startDate;
    private Date endDate;
    private static Map<String,String> statusMap=new HashMap<String, String>();

    @Autowired
    @Qualifier("cliamVouCherService")
    private CliamVouCherService cliamVouCherService;

    public String saveClaimVoucher(){
        claimVoucher.setCreator(this.getLoginEmployee());
        if(claimVoucher.getStatus().equals(Constants.CLAIMVOUCHER_SUBMITTED)){
            claimVoucher.setNexDeal((SysEmployee) this.getSession().get(Constants.AUTH_EMPLOYEE_MANAGER));
        }else{
            claimVoucher.setNexDeal(claimVoucher.getCreator());
        }
        claimVoucher.setDetailList(detailList);
        for (BizClaimVoucherDetail d : detailList){
            d.setClaimVoucher(claimVoucher);
        }
        cliamVouCherService.saveCliamVouCher(claimVoucher);
        return "redirectList";
    }

    public String searchClaimVoucher(){
        statusMap.put(Constants.CLAIMVOUCHER_CREATED,"新创建");
        statusMap.put(Constants.CLAIMVOUCHER_SUBMITTED,"已提交");
        statusMap.put(Constants.CLAIMVOUCHER_APPROVING,"待审批");
        statusMap.put(Constants.CLAIMVOUCHER_BACK,"已打回");
        statusMap.put(Constants.CLAIMVOUCHER_APPROVED,"已审批");
        statusMap.put(Constants.CLAIMVOUCHER_PAID,"已付款");
        statusMap.put(Constants.CLAIMVOUCHER_TERMINATED,"已终止");
        pageSupport=cliamVouCherService.getClimVoucherPage(this.getLoginEmployee().getSn(),
                status,startDate,endDate,pageNo,pageSize);
        return  "list";
    }


    public String getClaimVoucherById() {
        claimVoucher = cliamVouCherService.findClaimVoucherById(claimVoucher.getId());
        return "view";
    }

    public String toUpdate(){
        claimVoucher =  cliamVouCherService.findClaimVoucherById(claimVoucher.getId());
        return "update";
    }
    public String updateClaimVoucher(){
        if (claimVoucher.getStatus().equals(Constants.CLAIMVOUCHER_SUBMITTED)) {
            claimVoucher.setNexDeal((SysEmployee) this.getSession().get(
                    Constants.AUTH_EMPLOYEE_MANAGER));
        }
        claimVoucher.setDetailList(detailList);

        cliamVouCherService.updateClaimVoucher(claimVoucher);
        return "redirectList";
    }

    public String deleteClaimVoucherById(){
        cliamVouCherService.deleteClaimVoucher(claimVoucher);
        return "redirectList";
    }

    public BizClaimVoucher getClaimVoucher() {
        return claimVoucher;
    }

    public void setClaimVoucher(BizClaimVoucher claimVoucher) {
        this.claimVoucher = claimVoucher;
    }

    public List<BizClaimVoucherDetail> getDetailList() {
        return detailList;
    }

    public void setDetailList(List<BizClaimVoucherDetail> detailList) {
        this.detailList = detailList;
    }

    public CliamVouCherService getCliamVouCherService() {
        return cliamVouCherService;
    }

    public void setCliamVouCherService(CliamVouCherService cliamVouCherService) {
        this.cliamVouCherService = cliamVouCherService;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public static Map<String, String> getStatusMap() {
        return statusMap;
    }

    public static void setStatusMap(Map<String, String> statusMap) {
        ClaimVouCherAction.statusMap = statusMap;
    }
}
